import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
/**
 * Part of the expert mode steps. Displays all changeable options to the user
 * parts of the output to the user.
 * @author James Stanier
 */
public class ExpertGUI implements ActionListener {
	
	ArrayList<File> inputFiles;
	
	String paperType;
	String docType;
	String variableOutput;
	int fontSize;
	boolean titlePage;
	boolean inDepthSection;
	boolean tableOfContents;
	boolean keywordIndex;
	boolean codeIndex;
	boolean twoSidedPrinting;
	
	JFrame prevWindow;
	JFrame window;
	JCheckBox tocButton;
	JCheckBox titleButton;
	JCheckBox inDepthButton;
	JCheckBox keywordIndexButton;
	JCheckBox codeIndexButton;
	JCheckBox twoSidedButton;
	JButton nextButton;
	JButton backButton;
	
	JComboBox fontSelect;
	JComboBox paperSelect;
	JComboBox docSelect;
	JComboBox variableSelect;
	
	JLabel helpLabel;
	
	String outputDir;
	String outputFileName;
	
	/**
	 * Constructor.
	 * @param j The previous window.
	 * @param f The list of input files.
	 * @param s The output directory.
	 * @param t The output file name.
	 */
	public ExpertGUI(JFrame j, ArrayList<File> f, String s, String t) {
		outputFileName = t;
		outputDir = s;
		prevWindow = j;
		inputFiles = f;
		paperType = "a4paper";
		docType = "article";
		variableOutput = "none";
		fontSize = 10;
		twoSidedPrinting = false;
		titlePage = false;
		inDepthSection = false;
		tableOfContents = false;
		keywordIndex = false;
		codeIndex = false;
		expertScreen();
	}
	
	/**
	 * Configures and draws the GUI window.
	 */
	public void expertScreen() {
		window = new JFrame("JavaLiN - Expert mode");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(350,550);
		window.setLocationRelativeTo(null);
		window.toFront();
		window.setResizable(false);
		
		// Introductory passage and logo
		JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
		top.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		// Logo
		JLabel logo = new JLabel();
		logo.setIcon(new ImageIcon("logo.png"));
		logo.setHorizontalAlignment(JLabel.CENTER);
		top.add(logo, BorderLayout.NORTH);
		
		// Intro text
		JLabel intro = new JLabel("<HTML>Please select the output options you desire.</HTML>");
		intro.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		intro.setHorizontalAlignment(JLabel.CENTER);
		top.add(intro, BorderLayout.SOUTH);
		
		// Panel for radio buttons (1) and drop downs (2)
		JPanel bottom = new JPanel();
		bottom.setLayout(new GridLayout(2,0));
		
		// Panel for radio buttons
		JPanel tickBoxPanel = new JPanel();
		tickBoxPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		tickBoxPanel.setLayout(new GridLayout(6,0));
		tocButton = new JCheckBox("Table of contents");
		tocButton.addActionListener(this);
		tocButton.setToolTipText("Add a table of contents to the report.");
		tickBoxPanel.add(tocButton);
		
		titleButton = new JCheckBox("Title page");
		titleButton.addActionListener(this);
		titleButton.setToolTipText("Add a title page to the report.");
		tickBoxPanel.add(titleButton);
		
		inDepthButton = new JCheckBox("In-depth section");
		inDepthButton.addActionListener(this);
		inDepthButton.setToolTipText("Enable the in-depth commenting section.");
		tickBoxPanel.add(inDepthButton);
		
		keywordIndexButton = new JCheckBox("Keyword index");
		keywordIndexButton.addActionListener(this);
		keywordIndexButton.setToolTipText("Show an index of all marked-up keywords.");
		tickBoxPanel.add(keywordIndexButton);
		
		codeIndexButton = new JCheckBox("Code index");
		codeIndexButton.addActionListener(this);
		codeIndexButton.setToolTipText("Add a syntax highlighted representation of the source code.");
		tickBoxPanel.add(codeIndexButton);
		
		twoSidedButton = new JCheckBox("Two-sided printing");
		twoSidedButton.addActionListener(this);
		twoSidedButton.setToolTipText("Allow two-sided printing.");
		
		tickBoxPanel.add(twoSidedButton);
		
		bottom.add(tickBoxPanel, BorderLayout.NORTH);
		
		// Panel for drop down menus
		JPanel dropDownPanel = new JPanel();
		dropDownPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		dropDownPanel.setLayout(new GridLayout(4,4));
		
		// Combo boxes
		JLabel fontLabel = new JLabel("<HTML>Font size</HTML>");
		dropDownPanel.add(fontLabel);
		String[] fontSizes = {"10pt","11pt","12pt"};
		fontSelect = new JComboBox(fontSizes);
		fontSelect.addActionListener(this);
		fontSelect.setToolTipText("The font size to be used.");
		
		dropDownPanel.add(fontSelect);
		
		JLabel paperLabel = new JLabel("<HTML>Paper type</HTML>");
		dropDownPanel.add(paperLabel);
		String[] paperTypes = {"A4 paper","A5 paper","B5 Paper","Letter paper","Legal paper"};
		paperSelect = new JComboBox(paperTypes);
		paperSelect.addActionListener(this);
		paperSelect.setToolTipText("The type of paper to be used. If the report is not " +
				"to be printed, use A4 as default.");
		
		dropDownPanel.add(paperSelect);
		
		JLabel documentType = new JLabel("<HTML>Document type</HTML");
		dropDownPanel.add(documentType);
		String[] docTypes = {"Article","Report","Letter","Book","Slides"};
		docSelect = new JComboBox(docTypes);
		docSelect.addActionListener(this);
		docSelect.setToolTipText("The style of document to be created.");
		
		dropDownPanel.add(docSelect);
		
		JLabel variableLabel = new JLabel("<HTML>Variable display</HTML>");
		dropDownPanel.add(variableLabel);
		String[] variableTypes = {"None","Grid","Index"};
		variableSelect = new JComboBox(variableTypes);
		variableSelect.addActionListener(this);
		variableSelect.setToolTipText("Generate an index or grid of declared variables.");
		
		dropDownPanel.add(variableSelect);
		
		bottom.add(dropDownPanel, BorderLayout.SOUTH);
		
		// Buttons
		JPanel navButtons = new JPanel();
		navButtons.setLayout(new BorderLayout());
		navButtons.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		backButton = new JButton("<< Back");
		backButton.addActionListener(this);
		navButtons.add(backButton, BorderLayout.WEST);
		nextButton = new JButton("Create report");
		nextButton.addActionListener(this);
		navButtons.add(nextButton, BorderLayout.EAST);
		
		// Add to main window
		window.add("North", top);
		window.add("West", bottom);
		window.add("South", navButtons);
		window.validate();
		window.setVisible(true);
	}
	
	/**
	 * Describes actions to be taken upon component
	 * interaction.
	 */
	public void actionPerformed(ActionEvent e) {
		Object x = e.getSource();
		if(x.equals(tocButton)) {
			tableOfContents = tocButton.isSelected();
		}
		else if(x.equals(titleButton)) {
			titlePage = titleButton.isSelected();
			
			if(titleButton.isSelected()) {
				nextButton.setText("Next >>");
			}
			else {
				nextButton.setText("Create report");
			}
		}
		else if(x.equals(inDepthButton)) {
			inDepthSection = inDepthButton.isSelected();
		}
		else if(x.equals(keywordIndexButton)) {
			keywordIndex = keywordIndexButton.isSelected();
		}
		else if(x.equals(codeIndexButton)) {
			codeIndex = codeIndexButton.isSelected();
		}
		else if(x.equals(twoSidedButton)) {
			twoSidedPrinting = twoSidedButton.isSelected();
		}
		else if(x.equals(fontSelect)) {
			String temp = (String)fontSelect.getSelectedItem();
			if(temp.equals("10pt")) {
				fontSize = 10;
			}
			else if(temp.equals("11pt")) {
				fontSize = 11;
			}
			else if(temp.equals("12pt")) {
				fontSize = 12;
			}
		}
		else if(x.equals(paperSelect)) {
			String temp = (String)paperSelect.getSelectedItem();
			if(temp.equals("A4 paper")) {
				paperType = "a4paper";
			}
			else if(temp.equals("A5 paper")) {
				paperType = "a5paper";
			}
			else if(temp.equals("B5 paper")) {
				paperType = "b5paper";
			}
			else if(temp.equals("Letter paper")) {
				paperType = "letterpaper";
			}
			else if(temp.equals("Legal paper")) {
				paperType = "legalpaper";
			}
		}
		else if(x.equals(docSelect)) {
			String temp = (String)docSelect.getSelectedItem();
			if(temp.equals("Article")) {
				docType = "article";
			}
			else if(temp.equals("Report")) {
				docType = "report";
			}
			else if(temp.equals("Letter")) {
				docType = "letter";
			}
			else if(temp.equals("Book")) {
				docType = "book";
			}
			else if(temp.equals("Slides")) {
				docType = "slides";
			}
		}
		else if(x.equals(variableSelect)) {
			String temp = (String)variableSelect.getSelectedItem();
			if(temp.equals("None")) {
				variableOutput = "none";
			}
			else if(temp.equals("Index")) {
				variableOutput = "index";
			}
			else if(temp.equals("Grid")) {
				variableOutput = "grid";
			}
		}
		else if(x.equals(nextButton)) {
			// Here, if no title page has been selected, just continue.
			if(!titlePage) {
				OutputHandler oh = new OutputHandler(this, inputFiles, window);
			}
			else {
			// However, if they have selected a title page, we need to prompt
			// for the content in a new window
				TitleGUI tgui = new TitleGUI(this, window);
				window.setVisible(false);
			}
		}
		else if(x.equals(backButton)) {
			clearSettings();
			prevWindow.setVisible(true);
			window.setVisible(false);
		}
		else if(x.equals(nextButton)) {
			// Open next window...
		}
	}
	
	/**
	 * Get the input source file references.
	 * @return Input source files as File objects.
	 */
	public ArrayList<File> getInputFiles() {
		return inputFiles;
	}
	
	/**
	 * Get the current paper type.
	 * @return The paper type as a String.
	 */
	public String getPaperType() {
		return paperType;
	}
	
	/**
	 * Get the current document type.
	 * @return The document type as a String.
	 */
	public String getDocType() {
		return docType;
	}
	
	/**
	 * Get the current variable output setting.
	 * @return Variable output setting as a String.
	 */
	public String getVariableOutputOpt() {
		return variableOutput;
	}
	
	/**
	 * Get the currently selected font size.
	 * @return The font size as an integer.
	 */
	public int getFontSize() {
		return fontSize;
	}
	
	/**
	 * Get whether a title page has been requested.
	 * @return Whether a title page has been requested.
	 */
	public boolean isTitlePage() {
		return titlePage;
	}
	
	/**
	 * Get whether an in-depth section has been requested.
	 * @return Whether an in-depth section has been requested.
	 */
	public boolean isInDepthSection() {
		return inDepthSection;
	}
	
	/**
	 * Get whether a table of contents has been requested.
	 * @return Whether a table of contents has been requested.
	 */
	public boolean isTableOfContents() {
		return tableOfContents;
	}
	
	/**
	 * Get whether a keyword index has been requested.
	 * @return Whether a keyword index has been requested.
	 */
	public boolean isKeywordIndex() {
		return keywordIndex;
	}
	
	/**
	 * Get whether a code index has been requested.
	 * @return Whether a code index has been requested.
	 */
	public boolean isCodeIndex() {
		return codeIndex;
	}
	
	/**
	 * Get whether two sided printing has been requested.
	 * @return Whether two sided printing has been requested.
	 */
	public boolean isTwoSidedPrinting() {
		return twoSidedPrinting;
	}
	
	/**
	 * Get the output directory.
	 * @return The output directory.
	 */
	public String getOutputDir() {
		return outputDir;
	}
	
	/**
	 * Get the output file name.
	 * @return The output file name.
	 */
	public String getOutputFileName() {
		return outputFileName;
	}
	
	/**
	 * Get this screen's JFrame.
	 * @return JFrame belonging to this screen.
	 */
	public JFrame getWindow() {
		return window;
	}
	
	/**
	 * Clear all settings back to their original values.
	 */
	public void clearSettings() {
		paperType = "a4paper";
		docType = "article";
		variableOutput = "none";
		fontSize = 10;
		titlePage = inDepthSection = tableOfContents = keywordIndex = 
			codeIndex = twoSidedPrinting = false;
	}
}
